package widgets

import (
	"gitee.com/nanakura/imgui-restclient/db/model"
	g "github.com/AllenDang/giu"
	"github.com/imroc/req/v3"
)

type Pair struct {
	K string
	V string
}

type MyApp struct {
	headersPairs   []Pair
	paramsPairs    []Pair
	history        []model.History
	httpClient     *req.Client
	changerIdx     int
	changerValue   string
	tmpFilePath    string
	tmpParamsPairK string
	tmpParamsPairV string
	tmpHeaderPairK string
	tmpHeaderPairV string
	reqBodyEditor  *g.CodeEditorWidget
	reqResp        string
	requestTypes   []string
	contentTypes   []string
	url            string
	requestType    int32
	contentType    int32
}

func (app *MyApp) Init() {
	app.requestTypes = []string{"GET", "POST", "PUT", "DELETE"}
	app.contentTypes = []string{
		"application/json",
		"application/xml",
		"application/x-www-form-urlencoded",
		"text/plain",
		"text/xml",
		"text/html",
		"multipart/form-data",
		"application/xhtml+xml",
		"application/atom+xml",
		"application/svg+xml",
		"application/octet-stream",
	}
	app.httpClient = req.C().EnableDumpAllWithoutResponse()
	app.reqBodyEditor = g.CodeEditor().
		ShowWhitespaces(false).
		TabSize(2).
		Text("").
		LanguageDefinition(g.LanguageDefinitionCPP).
		Border(true).
		Size(g.Auto, 164)
	app.refreshHistory()
}

func (app *MyApp) Render() {
	g.SingleWindowWithMenuBar().Layout(
		g.PrepareMsgbox(),
		app.Menu(),
		g.TabBar().TabItems(
			app.RequestTabItem(),
			app.HistoryTabItem(),
		),
	)
}
